package com.echo.im.client.router.support;

import com.echo.im.client.handler.RouteSuccessHandler;
import com.echo.im.client.handler.RecvOfflineHandler;
import com.echo.im.client.handler.PushMessageHandler;
import com.echo.im.client.model.*;
import com.echo.im.client.router.MessageRouter;
import com.echo.im.client.status.UserStatusService;
import com.echo.im.client.model.IMUserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: echo
 * @time: 2024-08-23
 */
public class BaseMessageRouter implements MessageRouter<IMMessage> {

    private final UserStatusService userStatusService;
    private final RecvOfflineHandler recvOfflineHandler;
    private final RouteSuccessHandler routeSuccessHandler;
    private final PushMessageHandler pushMessageHandler;

    public BaseMessageRouter(UserStatusService userStatusService, RecvOfflineHandler recvOfflineHandler, RouteSuccessHandler routeSuccessHandler, PushMessageHandler pushMessageHandler) {
        this.userStatusService = userStatusService;
        this.recvOfflineHandler = recvOfflineHandler;
        this.routeSuccessHandler = routeSuccessHandler;
        this.pushMessageHandler = pushMessageHandler;
    }

    @Override
    public void routeMessage(IMMessage message) {
        // 根据每个接受者所连的IM-engin-server，进行分组
        Set<Long> offlineUserIdList = new HashSet<>();
        Map<Integer, List<IMUserInfo>> serverIdUserInfoMap=null;
        if (message.getIsNeedOfflineStore()){
            serverIdUserInfoMap = userStatusService.getServerIdUserInfoListMapWithOffline(message.getRecvIds(),offlineUserIdList);
        }else {
            serverIdUserInfoMap = userStatusService.getServerIdUserInfoListMap(message.getRecvIds());
        }

        // 逐个server发送
        serverIdUserInfoMap.forEach(
                (serverId, userInfoList) -> {
                    pushMessageHandler.pushMessage(message,userInfoList,serverId.toString(),false);
                }
        );

        // 埋点 接收方离线
        if (recvOfflineHandler!=null&&message.getIsNeedOfflineStore()){
            recvOfflineHandler.handle(offlineUserIdList, message);
        }

        // 埋点 消息投递成功
        if (routeSuccessHandler!=null){
            routeSuccessHandler.handle(message);
        }


        // 推送给自己的其他终端
        if (message.getSendToSelf()) {
            List<Integer> recvTerminals= IMTerminalType.codes().stream().filter(terminal -> !terminal.equals(message.getSender().getTerminal())).collect(Collectors.toList());
            Map<Integer, List<Integer>> serverIdMap = userStatusService.getServerIdTerminalListMap(message.getSender().getId(), recvTerminals);
            serverIdMap.forEach((serverId, terminalList)->{

                List<IMUserInfo> receivers = new ArrayList<>();
                for (Integer terminal : terminalList) {
                    receivers.add(new IMUserInfo(message.getSender().getId(), terminal));
                }
                pushMessageHandler.pushMessage(message,receivers,serverId.toString(),false);

            });
        }
    }
}
